import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class Test1 {
    public static void main(String[] args) {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "127.0.0.1:62001");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appPackage", "com.cnezsoft.xxm");
        capabilities.setCapability("appActivity", "com.cnezsoft.xxm.ui.LauncherActivity");
        capabilities.setCapability("noreset", true);

        AndroidDriver<MobileElement> driver = null;

        try {
            driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        // 等待喧喧加载完成
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 进入“消息”界面
        MobileElement messageButton = driver.findElement(By.xpath("//android.widget.TextView[@text='消息']"));
        messageButton.click();

        // 点击第一个对话窗口
        MobileElement firstConversation = driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]"));
        firstConversation.click();

        // 点击表情按钮
        MobileElement faceButton = driver.findElement(By.xpath("//android.widget.ImageView[@content-desc='表情']"));
        faceButton.click();

        // 选择第一个表情
        MobileElement firstEmoji = driver.findElement(By.xpath("//android.widget.GridView[1]/android.widget.LinearLayout[1]"));
        firstEmoji.click();

        // 发送
        MobileElement sendButton = driver.findElement(By.xpath("//android.widget.TextView[@text='发送']"));
        sendButton.click();

        // 关闭连接
        driver.quit();

    }
}
